package test.list;

public class PriorityQueue {
    public int[] elem;
    public int usedSize;

    public PriorityQueue() {
        elem = new int[3];
    }

    public void getString(){
        System.out.print("[");
        for(int i = 0;i < usedSize;i++){
            System.out.print(elem[i]);
            if(i != usedSize - 1) {
                System.out.print(",");
            }
        }
        System.out.println("]");
    }

    /**
     * 建堆的时间复杂度：
     *
     * @param array
     */
    public void createHeap(int[] array) {
        for(int i = 0;i < array.length;i++){
            //扩容
            if(usedSize >= elem.length){
                int[] arr = new int[usedSize * 2];
                for(int j = 0;j < usedSize;j++){
                    arr[j] = elem[j];
                }
                elem = arr;
            }
            elem[i] = array[i];
            usedSize++;
            shiftUp(i);
        }
    }

    /**
     *
     * @param root 是每棵子树的根节点的下标
     * @param len  是每棵子树调整结束的结束条件
     * 向下调整的时间复杂度：O(logn)
     */
    private void shiftDown(int root,int len) {
        if(len == 1){
            return;
        }
        int parent = root;
        int child = root * 2 + 1;
        while(child < len){
            if(child + 1 < len && elem[child] < elem[child + 1]){
                child++;
            }
            if(elem[parent] < elem[child]){
                int tmp = elem[parent];
                elem[parent] = elem[child];
                elem[child] = tmp;
            }
            parent = child;
            child = parent * 2 + 1;
        }
    }


    /**
     * 入队：仍然要保持是大根堆
     * @param val
     */
    public void push(int val) {
        //扩容
        if(usedSize >= elem.length){
            int[] arr = new int[usedSize * 2];
            for(int j = 0;j < usedSize;j++){
                arr[j] = elem[j];
            }
            elem = arr;
        }
        elem[usedSize] = val;
        usedSize++;
        shiftUp(usedSize - 1);
    }

    private void shiftUp(int child) {
        if(child == 0){
            return;
        }
        int parent = (child - 1) / 2;
        while(child > 0) {
            if(elem[parent] < elem[child]){
                int tmp = elem[parent];
                elem[parent] = elem[child];
                elem[child] = tmp;
            }
            child = parent;
            parent = (child - 1) / 2;
        }
    }

    public boolean isFull() {
        return usedSize == elem.length;
    }

    /**
     * 出队【删除】：每次删除的都是优先级高的元素
     * 仍然要保持是大根堆
     */
    public void pollHeap() {
        elem[0] = elem[usedSize - 1];
        usedSize--;
        shiftDown(0,usedSize);
    }

    public boolean isEmpty() {
        return usedSize == 0;
    }

    /**
     * 获取堆顶元素
     * @return
     */
    public int peekHeap() {
        return elem[0];
    }

    //测试
    public static void test(){
        System.out.println("创建堆");
        PriorityQueue queue = new PriorityQueue();
        int[] array = {2,3,4,1,5,6};
        queue.createHeap(array);
        queue.getString();

        System.out.println("入队");
        queue.push(5);
        queue.getString();

        System.out.println("出队");
        queue.pollHeap();
        queue.getString();

        System.out.println("查看栈顶元素");
        System.out.println(queue.peekHeap());

        System.out.println("查空");
        System.out.println(queue.isEmpty());

        System.out.println("查满");
        System.out.println(queue.isFull());
    }

    public static void main(String[] args) {
//        test();
    }
}
